home *** CD-ROM | disk | FTP | other *** search
/ com!online 2005 May / com_0505_1.iso / opensource / top10 / amc_install.exe / {app} / Scripts / Cinemagia.ro.ifs < prev    next >
Encoding:
Text File  |  2004-05-27  |  13.1 KB  |  484 lines

  1. // GETINFO SCRIPTING
  2. // Cinemagia.ro (RO) import with picture
  3.  
  4. (***************************************************
  5.  *  Movie importation script for:                  *
  6.  *      Cinemagia.ro (RO), http://www.cinemagia.ro *
  7.  *                                                 *
  8.  *  (c) 2004 Mihai Tudor                           *
  9.  *                          tekman@go.ro           *
  10.  *                                                 *
  11.  *  For use with Ant Movie Catalog 3.4.0           *
  12.  *  www.antp.be/software/moviecatalog              *
  13.  *                                                 *
  14.  *  The source code of the script can be used in   *
  15.  *  another program only if full credits to        *
  16.  *  script author and a link to Ant Movie Catalog  *
  17.  *  website are given in the About box or in       *
  18.  *  the documentation of the program               *
  19.  ***************************************************)
  20.  
  21. program Cinemagia;
  22.  
  23. var
  24.   MovieName: string;
  25.   MovieURL: string;
  26.  
  27. function FindLine(Pattern: string; List: TStringList; StartAt: Integer): Integer;
  28. var
  29.   i: Integer;
  30. begin
  31.   Result := -1;
  32.   if StartAt < 0 then
  33.     StartAt := 0;
  34.   for i := StartAt to List.Count-1 do
  35.     if Pos(Pattern, List.GetString(i)) <> 0 then
  36.     begin
  37.       Result := i;
  38.       Break;
  39.     end;
  40. end;
  41.  
  42. procedure AnalyzePage(Address: string);
  43. var
  44.   Page: TStringList;
  45. begin
  46.   Page := TStringList.Create;
  47.   Page.Text := GetPage(Address);
  48.  
  49.   if pos('Rezultatele cautarii', Page.Text) = 0 then
  50.   begin
  51.     AnalyzeMoviePage(Page)
  52.   end
  53.   else
  54.   begin
  55.     if pos('Nici un rezultat', Page.Text) > 0 then
  56.       ShowMessage('Sorry, no results were found for ' + MovieName)
  57.     else
  58.     begin
  59.       PickTreeClear;
  60.       AddMoviesTitles(Page);
  61.       if PickTreeExec(Address) then
  62.         AnalyzePage(Address);
  63.     end
  64.   end;
  65.   Page.Free;
  66. end;
  67.  
  68. function FindValue(BeginTag, EndTag: string; Page: TStringList; var LineNr: Integer; var Line: string): string;
  69. var
  70.   BeginPos, EndPos: Integer;
  71.   Value: string;
  72. begin
  73.   Result := '';
  74.   Value := '';
  75.   BeginPos := Pos(BeginTag, Line);
  76.   if BeginPos > 0 then
  77.   begin
  78.     BeginPos := BeginPos + Length(BeginTag);
  79.     if BeginTag = EndTag then
  80.     begin
  81.       Delete(Line,1,BeginPos-1);
  82.       BeginPos := 1;
  83.     end;
  84.     EndPos := pos(EndTag, Line);
  85.     while ((EndPos = 0) and (LineNr < Page.Count-1 )) do
  86.     begin
  87.       Value := Value + copy(Line, BeginPos, Length(Line) - BeginPos);
  88.       // Next Line
  89.       BeginPos := 1;
  90.       LineNr := LineNr + 1;
  91.       Line := Page.GetString(LineNr);
  92.       if Value = '' then
  93.         Exit;
  94.       EndPos := Pos(EndTag, Line);
  95.     end;
  96.     Value := Value + copy(Line, BeginPos, EndPos - BeginPos);
  97.    end;
  98.   Result := Value;
  99. end;
  100.  
  101. procedure AnalyzeMoviePage(Page: TStringList);
  102. var
  103.   Line, Value, Value2: string;
  104.   IsFirst, LineNr, BeginPos, EndPos, DescrImport: Integer;
  105.   HasPressReviews : Integer;
  106.   PressReviewAddress : string;
  107. begin
  108.   LineNr := FindLine('<h1 class="movie">', Page, 0);
  109.   if LineNr > 2 then
  110.   begin
  111.     Line := Page.GetString(LineNr);
  112.     BeginPos := pos('<a href="', Line);
  113.     if BeginPos > 0 then
  114.       BeginPos := BeginPos + 9;
  115.     EndPos := pos('style=', Line);
  116.     if EndPos = 0 then
  117.       EndPos := Length(Line);
  118.     Value := copy(Line, BeginPos, EndPos - BeginPos - 2);
  119.     HTMLDecode(Value);
  120.     MovieURL := 'http://www.cinemagia.ro' + Value;
  121.  
  122.     // URL
  123.     SetField(fieldURL, MovieURL);
  124.    
  125.     Value := FindValue( '<b>', '</b>', Page, LineNr, Line);
  126.     HTMLDecode(Value);
  127.     SetField(fieldTranslatedTitle, Value);
  128.  
  129.     Value := FindValue( '<i>(', ')</i>', Page, LineNr, Line);
  130.     HTMLDecode(Value);
  131.     SetField(fieldYear, Value);
  132.  
  133.     LineNr := LineNr + 1;
  134.     Line := Page.GetString(LineNr);
  135.  
  136.     Value := FindValue( '<i>', '</i>', Page, LineNr, Line);
  137.     HTMLDecode(Value);
  138.     SetField(fieldOriginalTitle, Value);
  139.   end;
  140.  
  141.   LineNr := FindLine('<img src="/getimg.php?id=', Page, LineNr);
  142.   if LineNr > -1 then
  143.   begin
  144.     Line := Page.GetString(LineNr);
  145.     BeginPos := pos('<img src="/getimg.php?id=', Line);
  146.     if BeginPos > 0 then
  147.       BeginPos := BeginPos + 10;
  148.     EndPos := pos('&size=s"', Line);
  149.     if EndPos = 0 then
  150.       EndPos := Length(Line);
  151.     Value := copy(Line, BeginPos, EndPos - BeginPos + 6) + 'm';
  152.     HTMLDecode(Value);
  153.  
  154.     GetMoviePicture( 'http://www.cinemagia.ro' + Value);
  155.   end;
  156.  
  157.   LineNr := FindLine('Regia<br>', Page, LineNr);
  158.   if LineNr > -1 then
  159.   begin
  160.     Line := Page.GetString(LineNr);
  161.     BeginPos := pos('Regia<br>', Line);
  162.     if BeginPos > 0 then
  163.       BeginPos := BeginPos + 9;
  164.      
  165.     Delete( Line, 1, BeginPos);
  166.     EndPos := pos('<br>', Line);
  167.     if EndPos = 0 then
  168.       EndPos := Length(Line);
  169.     Value := copy(Line, 1, EndPos);
  170.  
  171.     BeginPos := pos('">', Value) + 2;
  172.     EndPos := pos('</a>', Value);
  173.     Value := copy(Value, BeginPos, EndPos - BeginPos);
  174.  
  175.     HTMLDecode(Value);
  176.     SetField(fieldDirector, Value);
  177.   end;
  178.  
  179.   LineNr := FindLine('Gen<br>', Page, LineNr);
  180.   if LineNr > -1 then
  181.   begin
  182.     Line := Page.GetString(LineNr);
  183.     BeginPos := pos('Gen<br>', Line);
  184.     if BeginPos > 0 then
  185.       BeginPos := BeginPos + 7;
  186.  
  187.     Delete( Line, 1, BeginPos);
  188.     EndPos := pos('<br>', Line);
  189.     if EndPos = 0 then
  190.       EndPos := Length(Line);
  191.     Value := copy(Line, 1, EndPos);
  192.  
  193.     Value2 := '';
  194.     repeat
  195.       BeginPos := pos('">', Value) + 2;
  196.       EndPos := pos('</span>', Value);
  197.       Value2 := Value2 + ', ' + copy(Value, BeginPos, EndPos - BeginPos);
  198.       Delete( Value, 1, EndPos + 8);
  199.     until pos('">', Value) < 1;
  200.    
  201.     Delete( Value2, 1, 2);
  202.  
  203.     HTMLDecode(Value2);
  204.     SetField(fieldCategory, Value2);
  205.   end;
  206.  
  207.   LineNr := FindLine('what=cast', Page, LineNr);
  208.   if LineNr > -1 then
  209.   begin
  210.     Line := Page.GetString(LineNr);
  211.     BeginPos := pos('<a href="', Line);
  212.     if BeginPos > 0 then
  213.       BeginPos := BeginPos + 9;
  214.  
  215.     EndPos := pos('" class', Line);
  216.     if EndPos = 0 then
  217.       EndPos := Length(Line);
  218.     Value := copy(Line, BeginPos, EndPos - BeginPos);
  219.  
  220.     SetField(fieldActors, GetCast('http://www.cinemagia.ro' + Value));
  221.   end;
  222.  
  223.   HasPressReviews := 0;
  224.   IsFirst := 1;
  225.   Value2 := '';
  226.   LineNr := FindLine('Continuare</a>', Page, 0);
  227.   while (LineNr > -1) do
  228.   begin
  229.     Line := Page.GetString(LineNr);
  230.     BeginPos := pos('<a href="/movie.php', Line);
  231.     if BeginPos > 0 then
  232.       BeginPos := BeginPos + 9;
  233.      
  234.     EndPos := pos('&hist=0', Line);
  235.     if EndPos = 0 then
  236.       EndPos := Length(Line);
  237.     Value := copy(Line, BeginPos, EndPos - BeginPos + 7);
  238.     HTMLDecode(Value);
  239.  
  240.     if pos( 'what=pressreviews', Value) > 0 then
  241.     begin
  242.       PressReviewAddress := Value;
  243.       HasPressReviews := 1;
  244.     end
  245.     else
  246.     begin
  247.       if IsFirst = 1 then
  248.       begin
  249.         SetField(fieldDescription, GetDescriptions( Value));
  250.         IsFirst := 0;
  251.       end
  252.       else
  253.       begin
  254.         Value2 := Value2 + GetDescriptions( Value) + Chr(10) + Chr(13) + '===============';
  255.       end;
  256.     end;
  257.  
  258.     LineNr := FindLine('Continuare</a>', Page, LineNr + 1);
  259.   end;
  260.  
  261.   if HasPressReviews = 1 then
  262.   begin
  263.     Value2 := Value2 + GetPressReviews( PressReviewAddress);
  264.   end;
  265.  
  266.   SetField(fieldComments, Value2);
  267.  
  268.   DisplayResults;
  269. end;
  270.  
  271. procedure GetMoviePicture( PictureAddress: string);
  272. begin
  273.    GetPicture(PictureAddress, false);
  274. end;
  275.  
  276. function GetCast(Address: string): string;
  277. var
  278.   Line, Value: string;
  279.   LineNr: Integer;
  280.   NoInfiniteLoop : Integer;
  281.   BeginPos, EndPos: Integer;
  282.   Page: TStringList;
  283. begin
  284.   Result := '';
  285.   Page := TStringList.Create;
  286.   Page.Text := GetPage(Address);
  287.   LineNr := FindLine('Distributie', Page, 0);
  288.   Value := '';
  289.   if LineNr > -1 then
  290.   begin
  291.     LineNr := LineNr + 2;
  292.     Line := Page.GetString(LineNr);
  293.    
  294.     NoInfiniteLoop := 0;
  295.     while ( pos('</table>', Line) < 1 ) and ( NoInfiniteLoop < 10) do
  296.     begin
  297.       NoInfiniteLoop := NoInfiniteLoop + 1;
  298.       Line := Page.GetString(LineNr + 1);
  299.       BeginPos := pos('">', Line);
  300.       if BeginPos > 0 then
  301.         BeginPos := BeginPos + 2;
  302.       EndPos := pos('</a>', Line);
  303.       if EndPos < 1 then
  304.         EndPos := Length(Line);
  305.       if Value <> '' then
  306.         Value := Value + '; ';
  307.       Value := Value + copy(Line, BeginPos, EndPos - BeginPos);
  308.  
  309.       Line := Page.GetString(LineNr + 2);
  310.       BeginPos := pos('<td>', Line);
  311.       if BeginPos > 0 then
  312.         BeginPos := BeginPos + 4;
  313.       EndPos := pos('</td>', Line);
  314.       if EndPos < 1 then
  315.         EndPos := Length(Line);
  316.       if Value <> '' then
  317.         Value := Value + ' ';
  318.       Value := Value + '(in rolul ' + copy(Line, BeginPos, EndPos - BeginPos) + ')';
  319.    
  320.       Line := Page.GetString(LineNr + 4);
  321.       LineNr := LineNr + 4;
  322.     end;
  323.  
  324.     HTMLDecode(Value);
  325.  
  326.     Result := Value;
  327.   end;
  328.   Page.Free;
  329. end;
  330.  
  331. function GetDescriptions(Address: string): string;
  332. var
  333.   Line, Value: string;
  334.   LineNr: Integer;
  335.   BeginPos, EndPos: Integer;
  336.   Page: TStringList;
  337. begin
  338.   Result := '';
  339.   Page := TStringList.Create;
  340.   Page.Text := GetPage(Address);
  341.   LineNr := FindLine('Pagina principala', Page, 0);
  342.   if LineNr > -1 then
  343.   begin
  344.     Line := Page.GetString(LineNr + 1);
  345.     BeginPos := pos('<div class="section_title">', Line);
  346.     if BeginPos > 0 then
  347.       BeginPos := BeginPos + 27;
  348.     EndPos := pos('</div>', Line);
  349.     if EndPos < 1 then
  350.       EndPos := Length(Line);
  351.  
  352.     Value := copy(Line, BeginPos, EndPos - BeginPos);
  353.     HTMLRemoveTags(Value);
  354.     Value := '<b>' + Value + '</b><br/>';
  355.  
  356.     LineNr := LineNr + 2;
  357.     repeat
  358.       Line := Page.GetString(LineNr);
  359.       BeginPos := pos('<div class="section">', Line);
  360.       if BeginPos > 0 then
  361.         BeginPos := BeginPos + 21
  362.       else
  363.         BeginPos := 1;
  364.       EndPos := pos('</div>', Line);
  365.       if EndPos < 1 then
  366.         EndPos := Length(Line) + 1;
  367.       if Value <> '' then
  368.         Value := Value + ' ';
  369.       Value := Value + copy(Line, BeginPos, EndPos - BeginPos);
  370.       LineNr := LineNr + 1;
  371.     until (pos('</div>', Line) > 0) or (LineNr = Page.Count);
  372.     HTMLDecode(Value);
  373.  
  374.     Result := Value;
  375.   end;
  376.  
  377.   Page.Free;
  378. end;
  379.  
  380. function GetPressReviews(Address: string): string;
  381. var
  382.   Line, Value, Value2: string;
  383.   LineNr: Integer;
  384.   BeginPos, EndPos: Integer;
  385.   Page: TStringList;
  386. begin
  387.   Result := '';
  388.   Value2 := '';
  389.   Value := '';
  390.   Page := TStringList.Create;
  391.   Page.Text := GetPage(Address);
  392.   LineNr := FindLine('<div class="section_title">', Page, 0);
  393.   while LineNr > -1 do
  394.   begin
  395.     Line := Page.GetString(LineNr);
  396.     if pos('a href=', Line) < 1 then
  397.     begin
  398.       BeginPos := pos('<div class="section_title">', Line);
  399.       if BeginPos > 0 then
  400.         BeginPos := BeginPos + 27;
  401.       EndPos := pos('</div>', Line);
  402.       if EndPos < 1 then
  403.         EndPos := Length(Line);
  404.  
  405.       Value := copy(Line, BeginPos, EndPos - BeginPos);
  406.       HTMLRemoveTags(Value);
  407.       Value := '<b>' + Value + '</b><br/>';
  408.  
  409.       LineNr := LineNr + 2;
  410.       repeat
  411.         Line := Page.GetString(LineNr);
  412.         BeginPos := pos('<div class="section">', Line);
  413.         if BeginPos > 0 then
  414.           BeginPos := BeginPos + 21
  415.         else
  416.           BeginPos := 1;
  417.         EndPos := pos('</div>', Line);
  418.         if EndPos < 1 then
  419.           EndPos := Length(Line) + 1;
  420.         if Value <> '' then
  421.           Value := Value + ' ';
  422.         Value := Value + copy(Line, BeginPos, EndPos - BeginPos);
  423.         LineNr := LineNr + 1;
  424.       until (pos('</div>', Line) > 0) or (LineNr = Page.Count);
  425.    
  426.       Value2 := Value2 + Value;
  427.     end
  428.     else
  429.       LineNr := LineNr + 1;
  430.  
  431.     LineNr := FindLine('<div class="section_title">', Page, LineNr);
  432.   end;
  433.  
  434.   HTMLDecode(Value2);
  435.  
  436.   Result := Value2;
  437.  
  438.   Page.Free;
  439. end;
  440.  
  441. procedure AddMoviesTitles(Page: TStringList);
  442. var
  443.   Line: string;
  444.   LineNr: Integer;
  445.   MovieTitle, MovieAddress: string;
  446.   StartPos: Integer;
  447. begin
  448.   LineNr := FindLine( 'Rezultatele cautarii', Page, 0);
  449.   if LineNr > -1 then
  450.   begin
  451.     PickTreeAdd('Filme', '');
  452.     LineNr := LineNr + 3;
  453.     Line := Page.GetString(LineNr);
  454.     repeat
  455.       StartPos := pos('href="', Line) + 5;
  456.       Delete(Line, 1, StartPos);
  457.       MovieAddress := Copy(Line, 1, pos('"', Line) - 1);
  458.       StartPos := Pos('">', Line) + 2;
  459.       MovieTitle := Copy(Line, StartPos, Pos('</i>', Line) - StartPos);
  460.       HTMLDecode(Movietitle);
  461.       Delete( Movietitle, Pos('</a>', Movietitle), 4);
  462.       Delete( Movietitle, Pos('<i>', Movietitle), 3);
  463.       PickTreeAdd(MovieTitle, 'http://www.cinemagia.ro' + MovieAddress);
  464.       LineNr := LineNr + 1;
  465.       Line := Page.GetString(LineNr);
  466.     until Pos('</div>', Line) > 0;
  467.   end;
  468. end;
  469.  
  470. begin
  471.   if CheckVersion(3,4,0) then
  472.   begin
  473.     MovieName := GetField(fieldOriginalTitle);
  474.     if MovieName = '' then
  475.       MovieName := GetField(fieldTranslatedTitle);
  476.     if Input('Cinemagia.RO Import', 'Enter the title of the movie:', MovieName) then
  477.     begin
  478.       AnalyzePage('http://www.cinemagia.ro/search.php?q=' + UrlEncode(MovieName) + '&hist=0');
  479.     end;
  480.   end
  481.   else
  482.     ShowMessage('This script requires a newer version of Ant Movie Catalog (at least the version 3.4.0)');
  483. end.
  484.